﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;

namespace GamePadWPFTester
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        RotateTransform rotateTransform;
        ServiceReference1.GamePadServiceClient gamePadService;
        private BackgroundWorker worker;

        public MainWindow()
        {
            InitializeComponent();

            rotateTransform = new RotateTransform();
            rotateTransform.CenterX = image.Width / 2;
            rotateTransform.CenterY = image.Height / 2;
            rotateTransform.Angle = 0;

            image.RenderTransform = rotateTransform;

            gamePadService = new ServiceReference1.GamePadServiceClient();
            
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(RunOnBGThread);
            //worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGThreadWorkDone);

            worker.RunWorkerAsync();
            
        }
        private void RunOnBGThread(object sender, DoWorkEventArgs e)
        {

            int i = 0;
            try
            {
                while (true)
                {
                    System.Threading.Thread.Sleep(500);
                    Application.Current.Dispatcher.Invoke((ThreadStart)delegate

                                                        {
                                                            rotateTransform.Angle = gamePadService.GetAngle();
                                                        });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message, "Error");
            }

        }
    }
}
